package com.example.tse.rbhttpservice.service.account;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountAuthLoginRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountAuthLoginResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountBindMobileRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountBindMobileResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountForgetRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountForgetResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountIsNewUserRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountIsNewUserResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountLoginRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountLoginResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountLogoutRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountLogoutResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountRegisterRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountRegisterResponse;
import com.example.tse.rbhttpservice.service.account.model.AccountSendVcodeRequest;
import com.example.tse.rbhttpservice.service.account.model.AccountSendVcodeResponse;
import com.google.gson.Gson;

import org.kymjs.kjframe.http.HttpParams;





/**
 *
 * @author wliu
 *    edi  tse
 */
public class AccountServiceImpl implements AccountService {
	private Context context;
	private static Gson gson = new Gson();


	public AccountServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 验证第三方登录key是否已经绑定会员
	 * @param request
	 * @param listener
	 * @return AccountAuthLoginResponse
	 */

	    public void authLogin(AccountAuthLoginRequest request, final RemoteServiceListenerT< AccountAuthLoginResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("uid",request.getUid());
            params.put("type",request.getType());
            params.put("unionId",request.getUnionId());

        final String url = "account/authLogin";


       new HttpService().post(context, url, params,  AccountAuthLoginResponse.class, new ResponseServiceListenerT<RudderResponse>() {

                 @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok(( AccountAuthLoginResponse) rudderResponse);
            }

//            @Override
//            public void okData(RudderResponse rudderResponse) {
//                super.okData(rudderResponse);
//                listener.okData(rudderResponse == null ? new  AccountAuthLoginResponse() : ( AccountAuthLoginResponse) rudderResponse);
//            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new  AccountAuthLoginResponse() : ( AccountAuthLoginResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
            super.onMessage(message);
            listener.onMessage(message);
            }
        });
    }




	/**
	 * 绑定手机号，用于强制新绑定手机号码
	 * @param request
	 * @param listener
	 * @return AccountBindMobileResponse
	 */

	    public void bindMobile(AccountBindMobileRequest request, final RemoteServiceListenerT< AccountBindMobileResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("nickName",request.getNickName());
            params.put("phone",request.getPhone());
            params.put("uid",request.getUid());
            params.put("password",request.getPassword());
            params.put("vcode",request.getVcode());
            params.put("avatar",request.getAvatar());
            params.put("gender",request.getGender());
            params.put("source",request.getSource());
            params.put("unionId",request.getUnionId());

        final String url = "account/bindMobile";


            new HttpService().post(context, url, params, AccountBindMobileResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((AccountBindMobileResponse) rudderResponse);
            }

//            @Override
//            public void okData(RudderResponse rudderResponse) {
//                super.okData(rudderResponse);
//                listener.okData(rudderResponse == null ? new  AccountBindMobileResponse() : ( AccountBindMobileResponse) rudderResponse);
//            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new AccountBindMobileResponse() : (AccountBindMobileResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 用户忘记密码找回
	 * @param request
	 * @param listener
	 * @return AccountForgetResponse
	 */

	    public void forget(AccountForgetRequest request, final RemoteServiceListenerT< AccountForgetResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("phone",request.getPhone());
            params.put("vcode",request.getVcode());
            params.put("password",request.getPassword());

        final String url = "account/forget";


            new HttpService().post(context, url, params, AccountForgetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((AccountForgetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new AccountForgetResponse() : (AccountForgetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new AccountForgetResponse() : (AccountForgetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 判断是否是新用户
	 * @param request
	 * @param listener
	 * @return AccountIsNewUserResponse
	 */

	    public void isNewUser(AccountIsNewUserRequest request, final RemoteServiceListenerT< AccountIsNewUserResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "account/isNewUser";


            new HttpService().get(context, url, params, AccountIsNewUserResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((AccountIsNewUserResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new AccountIsNewUserResponse() : (AccountIsNewUserResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new AccountIsNewUserResponse() : (AccountIsNewUserResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 用户登陆
	 * @param request
	 * @param listener
	 * @return AccountLoginResponse
	 */

	    public void login(AccountLoginRequest request, final RemoteServiceListenerT< AccountLoginResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("phone",request.getPhone());
            params.put("password",request.getPassword());
            params.put("passwordEnc",request.getPasswordEnc());

        final String url = "account/login";


            new HttpService().post(context, url, params, AccountLoginResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((AccountLoginResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new AccountLoginResponse() : (AccountLoginResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new AccountLoginResponse() : (AccountLoginResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 退出登录
	 * @param request
	 * @param listener
	 * @return AccountLogoutResponse
	 */

	    public void logout(AccountLogoutRequest request, final RemoteServiceListenerT< AccountLogoutResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "account/logout";


            new HttpService().post(context, url, params, AccountLogoutResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((AccountLogoutResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new AccountLogoutResponse() : (AccountLogoutResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new AccountLogoutResponse() : (AccountLogoutResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 用户注册
	 * @param request
	 * @param listener
	 * @return AccountRegisterResponse
	 */

	    public void register(AccountRegisterRequest request, final RemoteServiceListenerT< AccountRegisterResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("phone",request.getPhone());
            params.put("vcode",request.getVcode());
            params.put("password",request.getPassword());

        final String url = "account/register";


            new HttpService().post(context, url, params, AccountRegisterResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((AccountRegisterResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new AccountRegisterResponse() : (AccountRegisterResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new AccountRegisterResponse() : (AccountRegisterResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 发送验证码（注册、找回密码用）
	 * @param request
	 * @param listener
	 * @return AccountSendVcodeResponse
	 */

	    public void sendVcode(AccountSendVcodeRequest request, final RemoteServiceListenerT< AccountSendVcodeResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("phone",request.getPhone());
            params.put("type",request.getType());

        final String url = "account/sendVcode";


            new HttpService().post(context, url, params, AccountSendVcodeResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((AccountSendVcodeResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new AccountSendVcodeResponse() : (AccountSendVcodeResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new AccountSendVcodeResponse() : (AccountSendVcodeResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




}